package com.ajaaxx.repo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.ajaaxx.datatypes.Question;
import com.ajaaxx.datatypes.User;
import com.ajaaxx.datatypes.Subject;
import com.ajaaxx.db.operations.DBOperations;
import com.ajaaxx.repo.EvaluationRepository;


@Repository("evaluationRepository")
public class EvaluationRepositoryImpl implements EvaluationRepository {

	public User getUserBasedOnCedulla(int cedulla) {
		return new DBOperations().getUserBasedOnCedulla(cedulla);
	}

	public Set<Question> getQuestions() {
		return new DBOperations().getQuestions();
	}

	public Set<Subject> getSubjects(User user) {
		return new DBOperations().getSubjects(user);
	}

	public boolean submitSurveyResult(User user, List<Question> questions) {
		Map<Integer,StringBuffer> processedQuestions = processQuestions(user,questions);
		return new DBOperations().submitSurveyResult(user, processedQuestions);
	}
	
	private Map<Integer,StringBuffer> processQuestions(User user,List<Question> questions){
		Map<Integer,Integer> relations = new DBOperations().fetchRelations(user.getCedulla(), user.getCurrentSemester());
		Map<Integer,StringBuffer> result = new HashMap<Integer, StringBuffer>();
		for(Question question:questions){
			Integer i ;
			if((i= relations.get(question.getSubjectId()))!=null){
				if(result.get(i)==null){result.put(i, new StringBuffer());}
				
				StringBuffer buff = result.get(i);
				buff.append(question.getQuestionId()+":"+question.getRating()+"*");
			}
		}
		
		return result;
		
	}

	

	

}
